package org.example.commerce.imp.controller;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.commerce.dao.entity.Category;
import org.example.commerce.imp.service.CategoryService;
import org.example.common.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;


@Slf4j
@CrossOrigin
@RestController
@Component
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    /**
     * 查询所有分类
     */
    @GetMapping("/{page}/{pageSize}")
    public R<PageInfo<Category>> getCategoryList(@PathVariable Integer page, @PathVariable Integer pageSize){
        log.info("get categoryList page = {}  pageSize ={}",page,pageSize);
        return categoryService.getCtaegoryList(page,pageSize);
    }

    /**
     * 新增分类
     */
    @PostMapping("/")
    public R<Category> categoryAdd(@RequestBody Category category){
        log.info("add new category data{}",category);
        return categoryService.addCategory(category);
    }

    /**
     * 删除商品分类
     */
    @DeleteMapping("/{id}")
    public R<String> deleteCategoryById(@PathVariable Long id){
        log.info("delete category by id = {}", id);
        return categoryService.deleteCategoryById(id);
    }

    /**
     * 通过id查询商品分类
     */
    @GetMapping("/{id}")
    public  R<Category> selectCategoryById(@PathVariable Long id){
        log.info("select category by id  = {}",id);
        return categoryService.selectCategoryById(id);
    }


    /**
     * 修改商品分类
     */
    @PutMapping("/")
    public R<Category> modifyCategory(@RequestBody Category category){
        return categoryService.modifyCategory(category);
    }

}
